package main

import (
	"fmt"
)

func main() {
	main := "goodgoogle";
	// sub := "google"
	// sub :="g"
	// sub := "e"
	// sub := "f";
	// sub := "oof"
	// sub := "ood"
	sub := "oog"
	idx := indexOf(main, sub);
	fmt.Println("idx----", idx);
}


func indexOf(main string, sub string) int{
	idxMain :=0
	idxSub :=0;
	lenMain := len(main)
	lenSub := len(sub)
	for{
		if idxMain > lenMain -1  ||  idxSub > lenSub -1{
			//主串匹配完  或者子串匹配完  结束循环
			break;
		}

		if main[idxMain] == sub[idxSub] {
			idxMain ++
			idxSub ++
		}else{
			idxMain = idxMain - idxSub +1
			idxSub = 0
		}
	}

	//匹配结束后
	if  idxSub > lenSub -1{
		return idxMain - lenSub
	}
	return -1;
}


